<?php

namespace common\models;

use common\consts\Consts;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Log;

/**
 * LogSearch represents the model behind the search form about `common\models\Log`.
 */
class LogSearch extends Log
{
    use DataTableQueryTrait;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id'], 'integer'],
            [['action', 'old_content', 'new_content', 'memo', 'all_content', 'created_by', 'created_at', 'updated_by', 'updated_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Log::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => Consts::DEFAULT_PAGE_SIZE,
            ],
            'sort'=> ['defaultOrder' => ['id'=>SORT_DESC]],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'action', $this->action])
            ->andFilterWhere(['like', 'old_content', $this->old_content])
            ->andFilterWhere(['like', 'new_content', $this->new_content])
            ->andFilterWhere(['like', 'memo', $this->memo])
            ->andFilterWhere(['like', 'all_content', $this->all_content])
            ->andFilterWhere(['like', 'created_by', $this->created_by])
            ->andFilterWhere(['like', 'updated_by', $this->updated_by]);

        return $dataProvider;
    }


    /**
     * @param $params array
     * @return array
     */
    public function search4Dt($params){
        $query = Log::find();
        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'action', $this->action])
            ->andFilterWhere(['like', 'old_content', $this->old_content])
            ->andFilterWhere(['like', 'new_content', $this->new_content])
            ->andFilterWhere(['like', 'memo', $this->memo])
            ->andFilterWhere(['like', 'created_by', $this->created_by])
            ->andFilterWhere(['like', 'updated_by', $this->updated_by]);

//        $query = $this->getDtQuery($query, $params, ['action', 'old_content', 'new_content', 'memo']);
        $query = $this->getDtQuery($query, $params, ['all_content']);

        $result = [
            'data' => $this->getDtListQuery($query, $params)->all(),
            'recordsTotal' => $query->count(),
            'recordsFiltered' => $query->count(),
        ];

        return $result;
    }
}
